﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="LocalResourceManager.cs" company="AzureContrib (Open source library http://azurecontrib.codeplex.com)">
//   Microsoft Public License (Ms-PL)
// </copyright>
// <summary>
//   An implementation of <see cref="ILocalResourceManager"/>
// </summary>
// --------------------------------------------------------------------------------------------------------------------

using System.IO;

using AzureContrib.WindowsAzure.ServiceRuntime;
using AzureContrib.WindowsAzure.StorageClient.Properties;

namespace AzureContrib.WindowsAzure.StorageClient
{
	/// <summary>
	/// An implementation of <see cref="ILocalResourceManager"/>
	/// </summary>
	public class LocalResourceManager : ILocalResourceManager
	{
		private readonly IRoleEnvironment roleEnvironment;

		/// <summary>
		/// Initializes a new instance of the LocalResourceManager class.
		/// </summary>
		/// <param name="roleEnvironment">The current <see cref="IRoleEnvironment"/></param>
		public LocalResourceManager(IRoleEnvironment roleEnvironment)
		{
			this.roleEnvironment = roleEnvironment;
		}

		/// <summary>
		/// Get a set of <see cref="ILocalResource"/>.
		/// </summary>
		/// <param name="localResourceCacheName">The name of the local resource to get data for.</param>
		/// <returns>The instance of the <see cref="ILocalResource"/>.</returns>
		/// <exception cref="CloudDriveManagerException">Thrown if the local resource could not be fetched.</exception>
		public ILocalResource GetLocalResource(string localResourceCacheName)
		{
			ILocalResource localResource;

			try
			{
				localResource = roleEnvironment.GetLocalResource(localResourceCacheName);
			}
			catch (RoleEnvironmentException ree)
			{
				throw new CloudDriveManagerException(Strings.UnableToGetLocalResourceNamed.F(localResourceCacheName), ree);
			}

			return new LocalResourceData(localResource);
		}

		private class LocalResourceData : ILocalResource
		{
			private readonly ILocalResource localResource;

			public LocalResourceData(ILocalResource localResource)
			{
				this.localResource = localResource;
			}

			public string Name
			{
				get
				{
					return localResource.Name;
				}
			}

			public DirectoryInfo RootPath
			{
				get
				{
					return localResource.RootPath;
				}
			}

			public int MaximumSizeInMegabytes
			{
				get
				{
					return localResource.MaximumSizeInMegabytes;
				}
			}
		}
	}
}